<template lang="">
    <div class="browseRecord">
        <div class="box">
            <el-steps :active="active" finish-status="success" simple style="margin-top: 20px">
                <el-step title="填写信息" >

                </el-step>
                <el-step title="上传照片" ></el-step>
                <el-step title="完成认证" ></el-step>
            </el-steps>
            <el-form ref="form" v-if="active==1" :model="form" label-width="80px">
                <el-form-item label="姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="学号">
                    <el-input v-model="form.stunumb"></el-input>
                </el-form-item>
                <el-form-item label="学校">
                    <el-input v-model="form.school"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="next1" >下一步</el-button>
                </el-form-item>
            </el-form>
            <el-form ref="form" v-if="active==2" :model="form" label-width="80px">
                <el-form-item label="正面照">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:10010/user/approve/head"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="form.head" :src="form.head" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="学生证">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:10010/user/approve/profile"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess2"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="form.profilePicture" :src="form.profilePicture" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="next2" >下一步</el-button>
                </el-form-item>
            </el-form>
            <el-empty image="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png" v-if="active==3" description="您已完成认证"></el-empty>
        </div>
    </div>
</template>
<script>
import { getUser,approve} from "../../../utils/user.js";
    export default {
        data() {
            return {
                params: {
                    userId: "",
                    token: "",
                },
                active: 1,
                form: {
                    name:'',
                    school:'',
                    stunumb:'',
                    head:'',
                    profilePicture:'',
                    userId:''
                }
            }
        },
        created() {
            this.params.userId = localStorage.getItem("user");
            this.params.token = this.$cookies.get("token");
            this.form.userId = localStorage.getItem("user");
            this.getUserInfo();
        },
        methods: {
            getUserInfo() {
                getUser(this.params).then((res) => {
                    if (res.data.code == 200) {
                        if(res.data.data.approveState=='已认证'){
                            this.active=3;
                        }
                    }
                });
            },
            next1() {
                if(this.form.name.length>0 && 
                   this.form.school.length>0 && 
                   this.form.stunumb.length>0){
                    this.active++;
                }else{
                    this.$message.warning("请完善信息！");
                }
            },
            next2() {
                if(this.form.head.length>0 && 
                   this.form.profilePicture.length>0 ){
                    approve(this.form,this.params).then((res) => {
                        if (res.data.code == 200) {
                            this.$message.success(res.data.msg);
                            this.active++;
                        }else{
                            this.$message.error(res.data.msg);
                        }
                    });
                }else{
                    this.$message.warning("请完善信息！");
                }
            },
            handleAvatarSuccess(res, file) {
                this.form.head = res.data;
            },
            handleAvatarSuccess2(res, file) {
                this.form.profilePicture = res.data;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>
<style lang="less" scoped>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
    @import url('./index.less');
</style>